<?php
class TplUserAdver {
	public static function TopMenu() {
//		$GLOBALS['jscripts'][] = '/js/module_adver.js';
		$GLOBALS['jscripts'][] = '/js/tabs.js';
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		$GLOBALS['head'][]='<script>//<![CDATA[
			$(function(){$("#sludinajumu_delis_tabs .sludinajumu_delis_tab").Tabs({OnBeforeSwitch: function(a){return true;}});});//]]></script>';
		return '<aside id="sludinajumu_delis_tabs">
		    <div data-rel="categories_tab" class="sludinajumu_delis_tab">
				<a href="#">'. $lang['adver_cat'] .'</a>
			</div>
			<div data-rel="list_tab" class="sludinajumu_delis_tab">
				<a href="#"><h1>'. $lang['list'] .'</h1></a>
			</div>
			<div data-rel="add_tab" class="sludinajumu_delis_tab">
				<a href="#">'. $lang['add_adver'] .'</a>
			</div>
		</aside>';
	}

	public static function BredCrumbs($navi = null) {
		global $Eleanor;
		if ($navi) {
			$ca = '';
			foreach ($navi as $k => $v) {
				$ca .= '<div class="sludinajuma_delis_footsteps_triangle"></div><span class="sludinajuma_delis_footsteps_grey"><a href="'. $Eleanor->Url->Construct(array('module' => $Eleanor->module['name']) + $Eleanor->Categories->GetUri($k), false, $GLOBALS['Eleanor']->Url->delimiter, 1) .'">'. $v .'</a></span>';
			}
		}
		$lang = Eleanor::$Language[$Eleanor->mconfig['n']];
		return '<div id="sludinajuma_delis_footsteps"><a href="'. $Eleanor->Url->Construct(array('module' => $Eleanor->module['name']), false, $GLOBALS['Eleanor']->Url->delimiter, 1) .'" title="'. htmlspecialchars($lang['adver']) .'"><span class="sludinajuma_delis_footsteps_orange">'. $lang['adver'] .'</span></a>
		'.(isset($ca) ? $ca : null).'</div>';
	}

	private static function NList($data) {
//		$GLOBALS['jscripts'][] = '/js/color.js';
		$GLOBALS['head']['colorbox'] = '<link rel="stylesheet" media="screen" href="/addons/colorbox/colorbox.css">';
		$c = '<section id="list_tab">';
//		$uid = Eleanor::$Login->GetUserValue('id');
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		foreach ($data['items'] as $v) {
			if (isset($v['image']) && file_exists(Eleanor::$rootf . $v['image'])) {
				$image = '<a class="gallery cboxElement" href='. $v['_url'] .'><img style="max-width:100px;max-height:100px;" src="'. $v['image'] .'" alt="'. $v['title'] .'"></a>';
			} else {
				$image = '';
			}
			$c .= Eleanor::$Template->BaseAdver(array(
				'top' => array(
					'published' => sprintf($lang['published_'], Eleanor::$Language->Date($v['date'], 'fdt')),
					'category' => isset($data['ucats'][$v['_cat']]) ?  '<a href="' . $data['ucats'][$v['_cat']] . '" title="' . strip_tags($GLOBALS['Eleanor']->Categories->dump[$v['_cat']]['description']) . '">' . $GLOBALS['Eleanor']->Categories->dump[$v['_cat']]['title'] . '</a>' : false,
				),
				'image' => isset($image) ? $image : null,
				'reads' => $v['reads'],
				'reads_lng' => $lang['reads'],
				'price_lng' => $lang['pr'],
				'category_lng' => $lang['category_'],
				'title' => '<a href="' . $v['_url'] . '">' . $v['title'] . '</a>',
				'text' => '<a href="' . $v['_url'] . '">'. Strings::CutStr($v['text'],200) .'</a>',
				'price' => $v['price'],
				'readmore' => '<a class="sludinajuma_delis_record_more" href="'. $v['_url'] .'#more">'. $lang['readmore'] .'</a>',
			));
		}
		return $c;
	}

	public static function AdverList($data, $cnt, $page, $pages, $pp, $links) {
		return static::TopMenu() . static::NList($data) . Eleanor::$Template->Pages($cnt, $pp, $page, array($links['pages'],$pages=>$links['first_page'])) .'</section>'. static::ShowCategories();
	}

	public static function AdverDateList($date, $data, $cnt, $page, $pp) {
		return static::TopMenu(reset($GLOBALS['title'])) . static::NList($data) . Eleanor::$Template->Pages($cnt, $pp, -$page, $GLOBALS['Eleanor']->Url->Construct(array('do' => $date, array('page' => '{page}')), true, ''));
	}

	public static function AdverAuthorList($data, $cnt, $page, $pp) {
		return static::TopMenu(reset($GLOBALS['title'])) . static::NList($data) . Eleanor::$Template->Pages($cnt, $pp, $page, $GLOBALS['Eleanor']->Url->Construct(array('do' => 'my', array('page' => '{page}')), true, ''));
	}

	public static function AdverCategoryList($category, $data, $cnt, $page, $pages, $pp, $links) {
//		$cu = $GLOBALS['Eleanor']->Categories->GetUri($category['id']);
		return static::TopMenu() . static::ShowCategories($category['id']) . static::Nlist($data) . Eleanor::$Template->Pages($cnt, $pp, $page, array($links['pages'], $pages => $links['first_page'])).'</section>';
	}

	public static function ShowCategories($cat = 0, $data = false) {
		$c = '<section id="categories_tab">';
		if (isset($GLOBALS['Eleanor']->Categories->dump[$cat])) {
			foreach($GLOBALS['Eleanor']->Categories->dump as $v) {
				if (in_array($GLOBALS['Eleanor']->Categories->dump[$cat]['id'], explode(',', $v['parents']))) {
					$c .= static::CategoryItem($v);
				}
			}
		} else {
			foreach($GLOBALS['Eleanor']->Categories->dump as $v) {
				$c .= static::CategoryItem($v);
			}
			return $c .'</section>'. ($data ? static::NList($data) : null);
		}
		return $c .'</section>';
	}

	public static function ShowAdver($arr, $category, $page, $navi, $comments) {
		$GLOBALS['jscripts'][] = '/js/tabs.js';
		$GLOBALS['jscripts'][] = '/js/jpreloader.min.js';
		 if (!isset($GLOBALS['head']['colorbox'])) {
			$GLOBALS['jscripts'][]='/addons/colorbox/jquery.colorbox-min.js';
			$GLOBALS['head']['colorbox'] = '<link rel="stylesheet" media="screen" href="/addons/colorbox/colorbox.css">';
		 }
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		if (isset($arr['images'])) {
			$so_image = count($arr['images']);
			$pic = '';
			foreach($arr['images'] as $img) {
				if (file_exists(Eleanor::$rootf . $img)) {
					if (strpos($img, '_preview') !== false) {
						$pa = str_replace('_preview', null, $img);
					} else {
						$pa = $img;
					}
					$pic .= '<a class="gallery cboxElement" href='. $pa .'><img class="sl_img" src="'. $img .'" alt="'. htmlspecialchars($arr['title'], ENT_QUOTES) .'"></a>';
				}
			}
		}
		return static::BredCrumbs($navi)
			   . Eleanor::$Template->BaseAdverFull(array(
			   'top' => array(
						  'published' => sprintf($lang['published_'], Eleanor::$Language->Date($arr['date'], 'fdt')),
						  'category' => $category ? sprintf($lang['category_'], '<a href="' . $GLOBALS['Eleanor']->Url->Construct($GLOBALS['Eleanor']->Categories->GetUri($category['id']), true, $GLOBALS['Eleanor']->Url->delimiter) . '" title="' . strip_tags($category['description']) . '">' . $category['title'] . '</a>') : false,
						  'author' => sprintf($lang['publisher_'], $arr['author_id'] ? '<a href="' . Eleanor::$Login->UserLink($arr['author'], $arr['author_id']) . '">' . $arr['author'] . '</a>' : $arr['author']),
						  'reads' => $lang['reads'] .' <span>'. $arr['reads']. '</span>',
					  ),
				'table' => array(
					'telefon'=> $arr['telefon'] ? '<tr><td class="sl_fir">'. $lang['tel'] .'</td><td class="sl_sec">'. $arr['telefon'] .'</td></tr>' : null,
					'webadres'=> $arr['webadres'] ? '<tr><td class="sl_fir">'. $lang['webadres'] .'</td><td class="sl_sec">'. $arr['webadres'] .'</td></tr>' : null,
					'pasta_index'=> $arr['pasta_index'] ? '<tr><td class="sl_fir">'. $lang['past'] .'</td><td class="sl_sec">'. $arr['pasta_index'] .'</td></tr>' : null,
					'name'=> $arr['name'] ? '<tr><td class="sl_fir">'. $lang['na'] .'</td><td class="sl_sec">'. $arr['name'].'</td></tr>' : null,
					'adres'=> $arr['adres'] ? '<tr><td class="sl_fir">'. $lang['adres'] .'</td><td class="sl_sec">'. $arr['adres'].'</td></tr>' : null,
					'region'=> $arr['region'] ? '<tr><td class="sl_fir">'. $lang['region'] .'</td><td class="sl_sec">'. $arr['region'].'</td></tr>' : null,
					'rajon'=> $arr['rajon'] ? '<tr><td class="sl_fir">'. $lang['rajon'] .'</td><td class="sl_sec">'. $arr['rajon'].'</td></tr>' : null,
					'price'=> $arr['price'] ? '<tr><td class="sl_fir">'. $lang['pr'] .'</td><td class="sl_sec">'. $arr['price'] .' Ls</td></tr>' : null,
				),
				'nap'=> '<a href="'.$GLOBALS['Eleanor']->Url->Construct(array('do'=>'em', 'id'=>$arr['id']), true, '').'">'.$lang['ema_sent'].'</a><br/>',
				'pic' => $pic,
				'pic_so' => ceil($so_image / 3),
				'title' => $arr['title'],
			   'text' => $arr['text'],
		   ));
	}

	public static function AddEdit($id, $values, $error) {
		$GLOBALS['jscripts'][] = '/js/ajaxupload.3.5.js';
		$GLOBALS['jscripts'][] = '/js/ajaxupload_adver.js';
		$GLOBALS['jscripts'][] = '/templates/AutoDiagnostika/js/dropDownNoScroll.js';
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		$g = '';
		$ml = array(
				  'title' => Eleanor::Input('title', $values['title'], array('id' => 'title', 'tabindex' => 1)),
				  'text' => Eleanor::Text('text', $values['text'], array('rows'=>10,'cols'=>50)),
				  'uri' => Eleanor::Input('uri', $values['uri'], array('onfocus' => 'if(!$(this).val())$(this).val($(\'#title\').val())', 'tabindex' => 2)),
			  );

		$Lst = Eleanor::LoadListTemplate('table-form');
//		$back = isset($_GET['noback']) ? '' : Eleanor::Control('back', 'hidden', isset($_POST['back']) ? (string) $_POST['back'] : getenv('HTTP_REFERER'));
		//TODO need to remake this shit
		$goroda = array('Liepājas rajons','Rēzeknes rajons','Aizkraukles rajons','Rīgas rajons','Rīga','Aluksnes rajons','Balvu rajons','Bauskas rajons','Cēsu rajons','Daugavpils rajons','Dobeles rajons','Gulbenes rajons','Jēkabpils rajons','Jelgavas rajons','Krāslavas rajons','Kuldīgas rajons','Limbažu rajons','Ludzas rajons','Madonas rajons','Ogres rajons','Preiļu rajons','Saldus rajons','Tukuma rajons','Talsu rajons','Valkas rajons','Valmieras rajons','Ventspils rajons');
		sort($goroda);

		if (isset($values['images']) && $values['images']) {
			$images = '';
			foreach($values['images'] as $v) {
				$images .= '<img src="'. $v .'" alt="">';
			}
		}
		$cat_s = array();
		foreach ($GLOBALS['Eleanor']->Categories->dump as $k => $v) {
			$cat_s += array($k => $v['title']);
		}

		return '<section class="sl_dl_form"  id="add_tab">'
				. ($error ? Eleanor::$Template->Message($error, 'error') : null)
				. '<form class="adver_form_add" action="'. $GLOBALS['Eleanor']->Url->Construct(array('do' => array('do' => 'add'))) .'" method="post" enctype="multipart/form-data">'
				. $Lst->begin()
				. $Lst->item($lang['maincat'], $Lst->select(array('_maincat', $cat_s)))
				. $Lst->item($lang['title'], $ml['title'])
				. $Lst->item(array($lang['na'],Eleanor::Input('name', $values['name'])))
				. $Lst->item(array($lang['ema'],Eleanor::Input('email', $values['email'])))
				. $Lst->item(array($lang['tel'],Eleanor::Input('telefon', $values['telefon'])))
				. $Lst->item(array($lang['webadres'],Eleanor::Input('webadres', $values['telefon'])))
				. $Lst->item($lang['region'], $Lst->select(array('region', $goroda)))
				. $Lst->item(array($lang['rajon'], Eleanor::Select('rajon', '',array('id' => 'city','disabled'=>'disabled')),'tr'=>array('style'=>'display:none;', 'id'=>'city1')))
				. $Lst->item(array($lang['adres'],Eleanor::Input('adres', $values['adres'])))
				. $Lst->item(array($lang['past'],Eleanor::Input('pasta_index', $values['pasta_index'])))
				. $Lst->item(array($lang['short_text'],$ml['text']))
				. $Lst->item(array($lang['pr'],Eleanor::Input('price', $values['price'])))
				. $Lst->item_row(array('<a href="#" id="sl_dl_form_im">+ Add Image</a>'))#TODO Translate
				. $Lst->item_img(isset($images) ? $images : null)
				. $Lst->item_row(array('<button>Send data</button>'))
				. $Lst->end()
				. $Lst->submitline(($id ? ' ' . Eleanor::Button(Eleanor::$Language['main']['delete'], 'button', array('onclick' => 'window.location=\'' . $GLOBALS['Eleanor']->Url->Construct(array('do' => 'delete', 'id' => $id), true, '') . '\'')) : ''))
				.'<div id="files_storage"></div>'.$Lst->endform().'<script>dropDownNoScroll.init("sl_dl_dropdown");</script></section>';
	}

	public static function Email($id, $values, $error) {
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];

		$Lst = Eleanor::LoadListTemplate('table-form');
		$back = isset($_GET['noback']) ? '' : Eleanor::Control('back', 'hidden', isset($_POST['back']) ? (string) $_POST['back'] : getenv('HTTP_REFERER'));

		return static::TopMenu()
				. ($error ? Eleanor::$Template->Message($error, 'error') : '')
				. $Lst->form()
					. $Lst->begin()
						. $Lst->item($lang['sub'], Eleanor::Input('sub', $values['title']))
						. $Lst->item($lang['ot'], Eleanor::Input('ot', $values['ot']))
						. $Lst->item($lang['short_text'], Eleanor::Text('text',$values['text'],array('rows'=>10,'cols'=>50)))
						. ($GLOBALS['Eleanor']->Captcha->disabled ? '' : $Lst->item($lang['captcha'], Eleanor::Input('check', '', array('tabindex' => 11)) . $GLOBALS['Eleanor']->Captcha->GetCode()))
					. $Lst->end()
					. $Lst->submitline($back . Eleanor::Button('Sent', 'submit', array('tabindex' => 12)))
				.$Lst->endform();
	}

	public static function AddEditComplete($back, $url, $edited, $status, $title, $mod) {
		$lang = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		if ($status) {
			if ($mod)
				$text = $edited ? $lang['moedit'] : $lang['modadd'];
			else
				$text = $edited ? $lang['nomodedit'] : $lang['nomodadd'];
		}
		else
			$text = $edited ? $lang['statusedit'] : $lang['statusadd'];
		return self::TopMenu(reset($GLOBALS['title']))
			. Eleanor::$Template->Message(sprintf($text, $title) . '<br><br>' . sprintf($lang['goto'], '<strong><a href="' . $GLOBALS['Eleanor']->Url->Construct($url) . '">' . $title . '</a>' . ($back ? ' | <a href="' . $back . '">' . $lang['pagewg'] . '</a></strong>' : '')), 'info');
	}

	public static function EmailC() {
		$l = Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']];
		return static::TopMenu(reset($GLOBALS['title']))
			   .Eleanor::$Template->RedirectScreen($GLOBALS['Eleanor']->Url->Prefix(true), 10)
			   .Eleanor::$Template->Message($l['sent'], 'info');
	}

	public static function DeleteAdver($arr, $back) {
		return static::TopMenu()
			. Eleanor::$Template->Confirm(sprintf(Eleanor::$Language[$GLOBALS['Eleanor']->mconfig['n']]['submit_del'], $arr['title']), $back);
	}

	public static function CategoryItem($v) {
		$v['count'] = current(Eleanor::$Db->Query('SELECT COUNT(*) AS `count_item` FROM `el_adver_l` AS `tm` WHERE  `tm`.`lcats` LIKE \'%,'. $v['id'] .',%\' AND `lstatus` = 1 LIMIT 1')->fetch_row());
		$image = $v['image'] ? $v['image'] : '/templates/AutoDiagnostika/images/sludinajumi_kategorijas/instruments.png';
		return '<div class="sludinajuma_delis_kategorijas">
			<a href="'. $GLOBALS['Eleanor']->Url->Construct($GLOBALS['Eleanor']->Categories->GetUri($v['id']), true, $GLOBALS['Eleanor']->Url->delimiter) .'">
				<div class="sludinajuma_delis_kategorijas_image_container">
					<div class="sludinajuma_delis_kategorijas_image_centering">
						<img src="'.  $image .'" alt="'. htmlspecialchars($v['title'], ENT_QUOTES) .'">
					</div>
				</div>
				<div class="sludinajuma_delis_kategorijas_text">'. $v['title'] .'<span>('. $v['count'] .')</span></div>
			</a>
		</div>';
	}
}